<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class Cart extends Model
{
    protected $fillable = [
        'customer_id',
        'product_id',
        'sku_id',
        'number'
    ];

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            if (!$model->number) {
                $model->number = 1;
            }
        });
    }

    public function product()
    {
        return $this->belongsTo(ProductSku::class, 'sku_id');
    }

    public function scopeRemove(Builder $builder, $customer_id, $product_id, $sku_id)
    {
        return $builder->whereCustomerId($customer_id)
            ->whereProductId($product_id)->whereSkuId($sku_id)->delete();
    }
}
